<?php
namespace app\admin\controller;

class About extends BaseAdmin
{
    public function index()
    {
        $re=db("about")->where("id=1")->find();
        $this->assign("re",$re);
        return \view("index");
    }
    public function save()
    {
        $re=db("about")->where("id=1")->find();
        if($re){
            $data=\input('post.');
            
            $res=db("about")->where("id=1")->update($data);
            if($res){
                $this->success("修改成功");
            }else{
                $this->error("修改失败");
            }
        }else{
            $this->error("非法操作");
        }
        
    }
    public function ideal()
    {
        $re=db("about")->where("id=3")->find();
        $this->assign("re",$re);
        return \view("ideal");
    }
    public function savel()
    {
        $re=db("about")->where("id=3")->find();
        if($re){
            $data=\input('post.');
    
            $res=db("about")->where("id=3")->update($data);
            if($res){
                $this->success("修改成功");
            }else{
                $this->error("修改失败");
            }
        }else{
            $this->error("非法操作");
        }
    
    }
    public function speak()
    {
        $re=db("about")->where("id=2")->find();
        $this->assign("re",$re);
        return \view("speak");
    }
    public function saves()
    {
        $re=db("about")->where("id=2")->find();
        $data=\input('post.');

        if(!\is_string(\input('image'))){
            $data['image']=\uploads("image");
        }else{
            $data['image']=$re['image'];
        }
        $res=db("about")->where("id=2")->update($data);
        if($res){
            $this->success("修改成功");
        }else{
            $this->error("修改失败");
        }
    }
    public function honor()
    {
        $list=db("honor")->order("sort asc")->paginate(20);
        $this->assign("list",$list);
        
        $page=$list->render();
        $this->assign("page",$page);
        
        return \view('honor');
    }
    public function add()
    {
        return \view("add");
    }
    public function saveh()
    {
        $data=\input('post.');
        if(!\is_string(\input('image'))){
            $data['image']=\uploads("image");
        }
        if(\input('status')){
            $data['status']=1;
        }
        $data['time']=\time();
        $re=db("honor")->insert($data);
        if($re){
            $this->success("添加成功");
        }else{
            $this->error("添加失败");
        }
    }
    public function sort(){
        $data=input('post.');
        $news=db('honor');
        foreach ($data as $id => $sort){
            $news->where(array('id' => $id ))->setField('sort' , $sort);
        }
        $this->redirect('honor');
    }
    public function delete_all(){
        $id=input('id');
        $arr=explode(",", $id);
        foreach ($arr as $v){
            $re=db('honor')->where("id=$v")->find();
            if($re){
               
                $del=db('honor')->where("id=$v")->delete();
    
            }else{
                return false;
            }
        }
        $this->redirect('honor');
    }
    public function change(){
        $id=\input('id');
        $re=db("honor")->where("id=$id")->find();
        if($re){
            if($re['status'] == 1){
                $res=db('honor')->where("id=$id")->setField("status",0);
                return $res;
            }elseif($re['status'] == 0){
                $res=db('honor')->where("id=$id")->setField("status",1);
                return $res;
            }
        }else{
            return false;
        }
        if($res){
            echo '1';
        }else{
            echo '0';
        }
    }
    public function delete(){
        $id=input('id');
        $re=db('honor')->where("id=$id")->find();
        if($re){
            $del=db('honor')->where("id=$id")->delete();
        }
        
        $this->redirect('honor');
    }
    public function modifys()
    {
        $id=\input('id');
        $re=db("honor")->where("id=$id")->find();
        $this->assign("re",$re);
        return \view('modifys');
    }
    public function usave()
    {
        $id=\input('id');
        $re=db("honor")->where("id=$id")->find();
        
        if($re){
            $data=\input('post.');
            if(!\is_string(\input('image'))){
                $data['image']=\uploads("image");
            }else{
                $data['image']=$re["image"];
            }
            if(\input('status')){
                $data['status']=1;
            }else{
                $data['status']=$re['status'];
            }
            $res=db("honor")->where("id=$id")->update($data);
            if($res){
                $this->success("修改成功",\url('honor'));
            }else{
                $this->error("修改失败",\url('honor'));
            }
        }else{
            $this->error("非法操作",\url('honor'));
        }
        
    }
    public function type()
    {
        $list=db("t_type")->order("t_sort asc")->select();
        $this->assign("list",$list);
        return \view('type');
    }
    public function save_type(){
        if($this->request->isAjax()){
            $id=$_POST['id'];
            if($id){
                $data['t_name']=$_POST['name'];
                $res=db("t_type")->where("t_id=$id")->update($data);
                if($res){
                    $this->success("修改成功！",url('type'));
                }else{
                    $this->error("修改失败！",url('type'));
                }
            }else{
                $data['t_name']=$_POST['name'];
                $re=db("t_type")->insert($data);
                if($re){
                    $this->success("添加成功！",url('type'));
                }else{
                    $this->error("添加失败！",url('type'));
                }
            }
    
        }else{
            $this->success("非法提交",url('type'));
        }
    }
    public function modify(){
        $id=input('id');
        $re=db('t_type')->where("t_id=$id")->find();
        echo json_encode($re);
    }
    public function delete_type()
    {
        $id=\input('id');
        $re=db("t_type")->where("t_id=$id")->find();
        if($re){
            $del=db("t_type")->where("t_id=$id")->delete();
            $res=db("tissue")->where("fid=$id")->select();
            if($res){
                $dels=db("tissue")->where("fid=$id")->delete();
            }
            $this->redirect('type');
        }else{
            $this->redirect('type');
        }
    }
    public function sorts(){
        $data=input('post.');
        $news=db('t_type');
        foreach ($data as $id => $sort){
            $news->where(array('t_id' => $id ))->setField('t_sort' , $sort);
        }
        $this->redirect('type');
    }
    public function lister(){
        $list=db('tissue')->alias('a')->join("t_type b","a.fid = b.t_id")->order('sort asc')->paginate(10);
        $this->assign("list",$list);
        $page=$list->render();
        $this->assign("page",$page);
        return view('lister');
    }
    public function adds()
    {
        $res=db("t_type")->select();
        $this->assign("res",$res);
        
        return \view('adds');
    }
    public function savet()
    {
        $data=\input('post.');
        if(!\is_string(\input('image'))){
            $data['image']=\uploads("image");
        }
        $re=db("tissue")->insert($data);
        if($re){
            $this->success("添加成功！",url('lister'));
        }else{
            $this->error("添加失败！",url('lister'));
        }
    }
    public function modifyss()
    {
        $id=\input('id');
        $re=db("tissue")->where("id=$id")->find();
        $this->assign("re",$re);
        $res=db("t_type")->select();
        $this->assign("res",$res);
        
        return \view('modifyss');
    }
    public function usavet()
    {
        $id=input('id');
        $re=db('tissue')->where("id=$id")->find();
        if($re){
            $data=\input('post.');
            if(!\is_string(\input('image'))){
                $data['image']=\uploads("image");
            }else{
                $data['image']=$re['image'];
            }
            $res=db("tissue")->where("id=$id")->update($data);
            if($res){
                $this->success("修改成功！",url('lister'));
            }else{
                $this->error("修改失败！",url('lister'));
            }
        }else{
            $this->error("非法操作",url('lister'));
        }
    }
    public function deletes(){
        $id=input('id');
        $re=db('tissue')->where("id=$id")->find();
        if($re){
            $del=db('tissue')->where("id=$id")->delete();
        }
        $this->redirect('lister');
    }
    public function sortss(){
        $data=input('post.');
        $news=db('tissue');
        foreach ($data as $id => $sort){
            $news->where(array('id' => $id ))->setField('sort' , $sort);
        }
        $this->redirect('lister');
    }
    
    
}